<template>
  <div>
    <el-form
      :model="tenant"
      :rules="rules"
      ref="tenantForm"
      label-width="100px"
      style="width: 70%"
    >
      <el-form-item :label="$t('common.name')" prop="name">
        <el-input v-model="tenant.name"></el-input>
      </el-form-item>
      <el-form-item :label="'管理员邮箱'" prop="adminEmailAddress" v-if="!id">
        <el-input v-model="tenant.adminEmailAddress"></el-input>
      </el-form-item>
      <el-form-item :label="'管理员密码'" prop="adminPassword" v-if="!id">
        <el-input type="password" v-model="tenant.adminPassword"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel" size="small">取消</el-button>
      <el-button type="primary" @click="handleSubmit" size="small"
        >确定</el-button
      >
    </div>
  </div>
</template>
<script>
import { getTenant, addTenant, updateTenant } from "@/api/tenant";
export default {
  components: {},
  data() {
    return {
      tenant: {
        name: "",
        id: null,
        adminEmailAddress:"",
        adminPassword:""
      },
      rules: {
        name: [
          {
            required: true,

            message: this.$t("tips.requireItem"),
            trigger: "blur",
          },
        ],
        adminEmailAddress: [
          {
            required: true,
            message: this.$t("tips.requireItem"),
            trigger: "blur",
          },
        ],
        adminPassword: [
          {
            required: true,
            message: this.$t("tips.requireItem"),
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    getData() {
      if (!this.id) return;
      getTenant(this.id).then((res) => {
        this.tenant = res || {};
      });
    },
    handleSubmit() {
      this.$refs["tenantForm"].validate((valid) => {
        if (valid) {
          this.saveData();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    saveData() {
      if (!this.id) {
        addTenant(this.tenant).then((res) => {
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success",
            position: "bottom-right",
          });
          this.$emit("success", res);
        });
      } else {
        updateTenant(this.tenant).then((res) => {
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success",
            position: "bottom-right",
          });
          this.$emit("success", res);
        });
      }
    },
    handleCancel() {
      this.$emit("close");
    },
  },
  created() {
    this.getData();
  },
  mounted() {
    //this.getUserData();
  },
  computed: {},
  props: {
    id: {
      type: String,
      default: null,
    },
  },
};
</script>
<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
